import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import scoreatpercentile
from scipy import stats
import scipy as sp
import csv
import random
from datetime import datetime
from time import strftime
import matplotlib.mlab as mlab

def mean_confidence_interval_data(data, confidence=0.95):
    a = 1.0*np.array(data)
    n = len(a)
    m, se = np.mean(a), np.std(a)/(n**0.5)
    # calls the inverse CDF of the Student's t distribution
    h = se * stats.t._ppf((1+confidence)/2., n-1)
    return m-h, m+h

def mean_confidence_interval(mean, std, n, confidence=0.95):
    m = mean
    se = std/(n**0.5)
    # calls the inverse CDF of the Student's t distribution
    h = se * stats.t._ppf((1+confidence)/2., n-1)
    return m-h, m+h

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    data2 = [441.82, 437.38, 445.92, 444.17, 444.89, 445.93, 443.97, 445.40, 445.95, 443.35, 441.95, 444.86, 438.96, 439.38]
    data2_22 =  [[ 199.0, 16.0],[  205.0, 16.0],[  196.0, 16.0],[  200.0, 16.0],[  218.0, 24.0],[  220.0, 24.0],
                 [  215.0, 24.0],[  223.0, 24.0],[  237.0, 32.0],[  234.0, 32.0],[  235.0, 32.0],[  230.0, 32.0],
                 [  250.0, 40.0],[  248.0, 40.0],[  253.0, 40.0],[  246.0, 40.0]]
    data2_22 = np.array(data2_22)

    data2_42 = [[13.9, 28.6], [16.0, 34.7], [10.3, 21.0], [11.8, 25.5], [16.7, 36.8], [12.5, 24.0], [10.0, 19.1], [11.4, 22.5],
                [13.9, 28.3], [12.2, 25.0], [15.4, 31.1], [14.8, 29.6], [14.9, 35.1], [12.9, 30.0], [15.8, 36.242]]
    data2_42 = np.array(data2_42)
    #y_hat = data2_22[:,1] * 2.03438 + 168.6
    #print y_hat - data2_22[:,0]
    fig = plt.figure()                 # set up plot
    ax = fig.add_subplot(1, 1, 1)
    #plt.xlim([15,45])
    plt.xlim([9,17])
    #plt.title('y - y_hat')
    plt.ylabel('Sales Price')
    plt.xlabel('Assesed Value')
    ax.plot(data2_42[:,0], data2_42[:,1], 'r*')
    #ax.plot(data2_22[:,1], y_hat - np.mean(data2_22[:,0]), 'g*')

    #plt.axhline(linewidth=1, color='r')
    
    plt.show()
    '''
    #x,y = np.random.randn(2,100)
    fig, axs = plt.subplots(nrows=2, ncols=1, sharex=True)
    ax = axs[0]
    n, bins, patches = ax.hist(data6a, 5, normed = 1, facecolor = 'green', alpha = 0.75)
    ax.set_title('LOW')
    
    ax = axs[1]
    n, bins, patches = ax.hist(data6b, 5, normed = 1, facecolor = 'green', alpha = 0.75)
    ax.set_title('MODERATE')
    
    # With 4 subplots, reduce the number of axis ticks to avoid crowding.
    ax.locator_params(nbins=4)
    plt.show()
    '''

    # confidence interval
    '''
    data5c = data5a
    for i in range(0, len(data5a)):
        data5c[i] = data5a[i] - data5b[i]
    '''
    #print mean_confidence_interval_data(data5c)
    # print mean_confidence_interval(13.2, 1.6, 796)
    
    #print (np.mean(data4) - 7)*(14**0.5)/ np.std(data4), np.mean(data5a), np.mean(data5b)

    # two-sided t-test
    # http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_1samp.html
    #print 't-statistic = %6.3f pvalue = %6.4f' %  stats.ttest_1samp(data2, 0)

    # paired samples two-sided t-test
    # http://docs.scipy.org/doc/scipy-0.7.x/reference/generated/scipy.stats.ttest_ind.html
    #print stats.ttest_ind(data6a, data6b)

    # stats.t.cdf: find the t-value with 1-alpha and degree of freedom
    # stats.t.cdf: find the 1 - alpha with t-value and degree of freedom
    #print stats.t.ppf(0.975, 19), stats.t.cdf(2.92, 2)
    #print 1.25555555556 - 2.09302405441*0.34458118395, 1.25555555556 + 2.09302405441*0.34458118395
    #plt.boxplot(data5a, notch=0, sym='b+', vert=1, whis=1.5,
                            #positions=None, widths=0.7, patch_artist=False, bootstrap=None, hold=None)
    #plt.show()
    '''
    # Q-Q plot
    values = data2
    fig = plt.figure()                 # set up plot
    ax = fig.add_subplot(1, 1, 1)
    (osm, osr), (m, b, r) = stats.probplot(values, dist='norm')  # compute
    osmf = osm.take([0, -1])  # endpoints
    osrf = m * osmf + b       # fit line
    plt.title('Normal Q-Q Plot')
    plt.ylabel('Precipitation Quantiles')
    plt.xlabel('Normal Theoretical Quantiles')
    ax.plot(osm, osr, '.', osmf, osrf, '-')
    
    plt.show()
    '''